package com.wkq.service.impl;

import com.wkq.dao.IPermissionDao;
import com.wkq.dao.IRoleDao;
import com.wkq.domain.Permission;
import com.wkq.domain.Role;
import com.wkq.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao iRoleDao;
    @Autowired
    private IPermissionDao iPermissionDao;

    @Override
    public int save(Role role) {
        int flag = iRoleDao.save(role);
        return flag;
    }

    @Override
    public int addPermissionToRole(String roleId, List<String> permissionId) {
        int flag = iRoleDao.addPermissionToRole(roleId, permissionId);
        return flag;
    }

    @Override
    public Role findById(int id) {
//        根据角色id查询出角色相关信息
        Role role = iRoleDao.findById(id);
//        根据查询出来的角色的id查询该角色拥有的资源权限
        List<Permission> permissionList = iPermissionDao.findAllByRoleId(role.getId());
        role.setPermissions(permissionList);
        return role;
    }

    @Override
    public List<Role> findOtherRole(String userId) {
        List<Role> otherRoleList = iRoleDao.findOtherRole(userId);
        return otherRoleList;
    }

    @Override
    public List<Role> findAllByUserId(String userId) {
        List<Role> roleList = iRoleDao.findAllByUserId(userId);
        return roleList;
    }

    @Override
    public List<Role> findAll() {
        List<Role> roleList = iRoleDao.findAll();
        return roleList;
    }
}
